# from db import Base, engine
# from typing import List
# from sqlalchemy import String, insert, update, select, delete, ForeignKey
# from sqlalchemy.orm import Mapped, mapped_column, relationship, sessionmaker, Session
# from industry.dtos import ProducerDto, CarDto
#
# from common import PageParams, PageResult
#
#
# # Session = sessionmaker(engine)
#
# class Producer(Base):
#     __tablename__ = "producer"
#
#     id: Mapped[int] = mapped_column(primary_key=True, nullable=False)
#     name: Mapped[str] = mapped_column(String(200))
#     nation_id: Mapped[int] = mapped_column(ForeignKey("nation.id"))
#
#     nation: Mapped["Nation"] = relationship(lazy=False)
#
#
# class Car(Base):
#     __tablename__ = "industry"
#
#     id: Mapped[int] = mapped_column(primary_key=True, nullable=False)
#     name: Mapped[str] = mapped_column(String(200))
#     producer_id: Mapped[int] = mapped_column(ForeignKey("producer.id"))
#     user_id: Mapped[int] = mapped_column(ForeignKey("user.id"))
#
#     producer: Mapped[Producer] = relationship(lazy=False)
#     # user: Mapped[User] = relationship()
#
#
# Base.metadata.create_all(engine)
#
#
# def producer_dto2model(item: ProducerDto):
#     producer = Producer(id=item.id, name=item.name, nation_id=item.nation_id)
#     # if (item.nation_id == None or item.nation_id == 0) and item.nation_name:
#     #     nation = Nation(name=item.nation_name, location="")
#     #     producer.nation = nation
#     return producer
#
#
# def car_dto2model(item: CarDto):
#     car = Car(id=item.id, name=item.name, producer_id=item.producerId, user_id=item.userId)
#     return car
#
#
# # =====================   producer   =======================
#
#
# def insert_producer(item: ProducerDto):
#     producer = producer_dto2model(item)
#     with Session(engine) as session, session.begin():
#         session.add(producer)
#
#
# def update_producer(item: ProducerDto):
#     with Session(engine) as session, session.begin():
#         return session.query(Producer).filter(Producer.id == item.id).update(
#             {"name": item.name, "nation_id": item.nationId}
#         )
#
#
# def delete_producer(id: int):
#     with Session(engine) as session, session.begin():
#         return session.query(Producer).filter(Producer.id == id).delete()
#
#
# def get_producer(id: int):
#     with Session(engine) as session:
#         producer = session.query(Producer).filter(Producer.id == id).first()
#         return ProducerDto.from_orm(producer)
#
#
# def producer_page(params: PageParams):
#     with Session(engine) as session:
#         offset = (params.page - 1) * params.size
#         sql_where = []
#         if not params and not params.params['name'] is None:
#             sql_where.append(Producer.name.contains(params.params['name']))
#         query = session.query(Producer).where(*sql_where)
#         results = query.offset(offset).limit(params.size).all()
#         data = []
#         for result in results:
#             data.append(ProducerDto.from_orm(result))
#
#         total = query.count()
#         return PageResult(page=params.page, size=params.size, total=total, list=data)
#
#
# # =====================   car   =======================
#
#
# def insert_car(item: CarDto):
#     car = car_dto2model(item)
#     with Session(engine) as session, session.begin():
#         session.add(car)
#
#
# def update_car(item: CarDto):
#     with Session(engine) as session, session.begin():
#         return session.query(Car).filter(Car.id == item.id).update(
#             {"name": item.name, "producer_id": item.producerId, "user_id": item.userId}
#         )
#
#
# def delete_car(id: int):
#     with Session(engine) as session, session.begin():
#         return session.query(Car).filter(Car.id == id).delete()
#
#
# def get_car(id: int):
#     with Session(engine) as session:
#         car = session.query(Car).filter(Car.id == id).first()
#         return CarDto.from_orm(car)
#
#
# def car_page(params: PageParams):
#     with Session(engine) as session:
#         offset = (params.page - 1) * params.size
#         sql_where = []
#         if not params and not params.params['name'] is None:
#             sql_where.append(Car.name.contains(params.params['name']))
#         query = session.query(Car).where(*sql_where)
#         results = query.offset(offset).limit(params.size).all()
#         data = []
#         for result in results:
#             data.append(CarDto.from_orm(result))
#
#         total = query.count()
#         return PageResult(page=params.page, size=params.size, total=total, list=data)
